﻿using Cariwisata.Data;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.System;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace Cariwisata
{
    /// <summary>
    /// A page that displays details for a single item within a group while allowing gestures to
    /// flip through other items belonging to the same group.
    /// </summary>
    public sealed partial class ItemDetailPage : Cariwisata.Common.LayoutAwarePage
    {
        // Attribute
        private string selectedValue = String.Empty;
        private Achievement selectedAchievement = null;
        private TempatWisata selectedWisata = null;
        private SampleDataItem item = null;

        // Constructor
        public ItemDetailPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // Allow saved page state to override the initial item to display
            if (pageState != null && pageState.ContainsKey("SelectedItem"))
            {
                navigationParameter = pageState["SelectedItem"];
            }

            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            this.item = SampleDataSource.GetItem((String)navigationParameter);
            this.DataContext = item;
            this.ItemDetailScrollViewer.DataContext = item;
            this.snapPanel.DataContext = item;

            this.TempatWisata0.DataContext = item.TempatWisata[0];
            this.TempatWisataS0.DataContext = item.TempatWisata[0];
            if (item.TempatWisata.Count > 1)
            {
                this.TempatWisata1.DataContext = item.TempatWisata[1];
                this.TempatWisataS1.DataContext = item.TempatWisata[1];
            }
            if (item.TempatWisata.Count > 2)
            {
                this.TempatWisata2.DataContext = item.TempatWisata[2];
                this.TempatWisataS2.DataContext = item.TempatWisata[2];
            }
            //this.flipView.SelectedItem = item;
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            //var selectedItem = (SampleDataItem)this.flipView.SelectedItem;
            //pageState["SelectedItem"] = selectedItem.Nama; 
        }

        Brush prevTabColor;
        private void Tab_PointerEntered(object sender, PointerRoutedEventArgs e)
        {
            prevTabColor = ((Button)sender).Foreground;
            ((Button)sender).Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 50, 50, 255));
        }

        private void Tab_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            ((Button)sender).Foreground = prevTabColor;
        }

        private void FullWidth(object sender, RoutedEventArgs e)
        {
            ((ScrollViewer)sender).Width = Frame.Width;
        }

        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            //SampleDataItem item = ((SampleDataItem)this.itemListView.SelectedItem);
            //this.Frame.Navigate(typeof(ImagePage), item.Nama);
        }

        private void OnMapButtonClicked(object sender, RoutedEventArgs e)
        {
            //SampleDataItem item = ((SampleDataItem)this.itemListView.SelectedItem);
            //this.Frame.Navigate(typeof(MapPage), item.Nama);    
        }

        private async void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            ClosePopupStoryboard.Begin();

            await Task.Delay(300);
            PopupQuestion.Visibility = Visibility.Collapsed;
            backButton.IsEnabled = true;
            ItemDetailScrollViewer.IsEnabled = true;

            if (this.selectedAchievement.Jawaban.Equals(this.selectedValue))
            {
                this.selectedAchievement.Status = true;
                if (this.selectedWisata.UpdateStatus())
                {
                    this.item.UpdateStatus();
                }
                new MessageDialog("Benar!").ShowAsync();
            }
            else
            {
                new MessageDialog("Salah!").ShowAsync();
            }
        }

        private void AchievementListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as ListBox).SelectedIndex != -1)
            {
                if (!((sender as ListBox).SelectedItem as Achievement).Status)
                {
                    PopupQuestion.Visibility = Visibility.Visible;
                    PopupStoryboard.Begin();
                    this.selectedAchievement = ((sender as ListBox).SelectedItem as Achievement);
                    this.selectedWisata = (TempatWisata)(sender as ListBox).DataContext;
                    this.PopupStackPanel.DataContext = selectedAchievement;
                    ItemDetailScrollViewer.IsEnabled = false;
                    backButton.IsEnabled = false;
                }
                (sender as ListBox).SelectedIndex = -1;
            }
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            this.selectedValue = (string)(sender as RadioButton).Content;
        }

        private void TourName_Click(object sender, RoutedEventArgs e)
        {
            Launcher.LaunchUriAsync(new Uri((sender as TravelInfo).Link));
        }

        private void HotelName_Click(object sender, RoutedEventArgs e)
        {
            Launcher.LaunchUriAsync(new Uri((sender as HotelInfo).Link));
        }
    }
}
